package com.grad.webstudy.controller;

import com.grad.webstudy.constants.RedisConstants;
import com.grad.webstudy.constants.TokenConstants;
import com.grad.webstudy.enums.CodeEnum;
import com.grad.webstudy.enums.CourseLevelEnums;
import com.grad.webstudy.service.RedisService;
import com.grad.webstudy.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Description: 公共类，比如上传文件之类
 * @Author: zjf
 * @Date: 2020/1/27 16:40.
 */

@RestController
@RequestMapping("/common")
public class CommonController {


    @Autowired
    private RedisService redisService;


    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 获取热词
     * @return
     */
    @GetMapping("/redis/getHotWord")
    public Map<String,Object> queryHotWord(){

        return redisService.getHotWord(10);
    }


    /**
     * 获取当前在线用户数量
     * @return
     */
    @GetMapping("/redis/getLoginCount")
    public Integer queryLoginCount(){


        /**
         * 统计项包括：当前用户数量，热词，网站访问量，活跃用户量，总用户数量，
         */

        return redisTemplate.keys(TokenConstants.TOKEN_LOGIN_CACHE_KEY_PREFIX+"*").size();
    }

    /**
     * 网站访问量+1
     * 每点击一次就增加一个访问量
     */
    @GetMapping("/site/addVisit")
    public void addVisit(){
        String time= DateUtils.toString(new Date(), RedisConstants.REDIS_TOTAL_VIIST_KEY_FORMAT);

        redisTemplate.opsForHash().increment(RedisConstants.REDIS_TOTAL_KEY,time,1000);
    }


    @GetMapping("/site/getVisit")
    public Object getVisit(){
        List<String> dateList=DateUtils.getPastDate(RedisConstants.REDIS_TOTAL_VIIST_KEY_FORMAT,7);
        Map<String,List<String>> map=new HashMap<>();
        map.put("date",dateList);
        map.put("result",redisTemplate.opsForHash().multiGet(RedisConstants.REDIS_TOTAL_KEY,dateList));
        return map;

    }



    @GetMapping("/getLang")
    public List<Map<String, Object>> getLang(){
        List<Map<String,Object>>  langs = new ArrayList<>();
        for (CodeEnum codeEnum : CodeEnum.values()) {
            Map code=new HashMap();
            code.put("id",codeEnum.getCodeNumber());
            code.put("type",codeEnum.getType());
            langs.add(code);
        }
        return langs;
    }


    @GetMapping("/getLevel")
    public List<Map<String, Object>> getLevel(){
        List<Map<String,Object>>  levels = new ArrayList<>();
        for (CourseLevelEnums courseLevelEnums : CourseLevelEnums.values()) {
            Map level =new HashMap();
            level.put("id",courseLevelEnums.getLevel());
            level.put("typeName",courseLevelEnums.getLevelDesc());
            levels.add(level);
        }
        return levels;
    }



}
